﻿package mortal.game.view.mainUI.shortcutbar
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.manager.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.font.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class ShortcutBarItem extends CDItem
    {
        private var indexLabel:GTextFiled;
        private var _selectFilter:ScaleBitmap;
        private var _isShowBg:Boolean;
        private var _txtSkillTypeName:GTextFiled;
        private var _txtNum:GTextFiled;
        private var _bmpCanNotUse:Bitmap;
        private var _lockedIcon:GBitmap;
        private var _isLocked:Boolean = false;
        private var _isDraging:Boolean = false;
        private static var _bmpdCanNotUse:BitmapData;

        public function ShortcutBarItem(param1:int = 0)
        {
            this._pos = param1;
            _isShowLeftTimeEffect = true;
            _isShowFreezingEffect = true;
            this.buttonMode = true;
            this.useHandCursor = true;
            this.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOverHandler);
            this.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseOutHandler);
            this.addEventListener(MouseEvent.CLICK, this.onShortcutsClickHandler);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.onSlortcutsDownHandler);
            this.addEventListener(MouseEvent.MOUSE_UP, this.mouseupHandler);
            this.name = "shortCutBar_Pos" + param1.toString();
            return;
        }// end function

        public function setLock(param1:Boolean) : void
        {
            if (param1 == this._isLocked)
            {
                return;
            }
            this._isLocked = param1;
            this.updateLockStatus();
            return;
        }// end function

        private function updateLockStatus() : void
        {
            this.isDragAble = !this._isLocked;
            this.isThrowAble = !this._isLocked;
            if (this._isLocked)
            {
                if (this.dragSource != null)
                {
                    this.isDropAble = false;
                }
                else
                {
                    this.isDragAble = true;
                }
                if (this._lockedIcon == null)
                {
                    this._lockedIcon = UIFactory.bitmap(ImagesConst.LockIcon, 2, 4, this);
                    this._lockedIcon.x = _width - 3 - this._lockedIcon.width;
                }
                if (this._lockedIcon.parent == null && this.dragSource != null)
                {
                    this.addChild(this._lockedIcon);
                }
                else if (this.dragSource == null)
                {
                    DisplayUtil.removeMe(this._lockedIcon);
                }
            }
            else
            {
                this.isDropAble = true;
                DisplayUtil.removeMe(this._lockedIcon);
            }
            return;
        }// end function

        private function onSlortcutsDownHandler(event:MouseEvent) : void
        {
            this._isDraging = false;
            this.addEventListener(MouseEvent.MOUSE_MOVE, this.onShortcutMoveHandler);
            return;
        }// end function

        private function mouseupHandler(event:MouseEvent) : void
        {
            this.removeEventListener(MouseEvent.MOUSE_MOVE, this.onShortcutMoveHandler);
            return;
        }// end function

        private function onShortcutMoveHandler(event:MouseEvent) : void
        {
            this._isDraging = true;
            return;
        }// end function

        public function onShortcutsClickHandler(event:MouseEvent) : void
        {
            if (!this._isDraging)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarClicked, _pos));
            }
            return;
        }// end function

        private function addCanNotAttackMask() : void
        {
            if (!this._bmpCanNotUse)
            {
                this._bmpCanNotUse = new Bitmap();
                this._bmpCanNotUse.x = 3;
                this._bmpCanNotUse.y = 3;
                this._bmpCanNotUse.bitmapData = bmpdCanNotUse;
            }
            this.addChild(this._bmpCanNotUse);
            return;
        }// end function

        private function removeCanNotAttackMask() : void
        {
            if (this._bmpCanNotUse && contains(this._bmpCanNotUse))
            {
                removeChild(this._bmpCanNotUse);
            }
            return;
        }// end function

        override public function setSize(param1:Number, param2:Number) : void
        {
            super.setSize(param1, param2);
            return;
        }// end function

        override public function get bitmapdata() : BitmapData
        {
            return super.bitmapdata;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._selectFilter = ResourceConst.getScaleBitmap(ImagesConst.selectFilter, -2, -2, 46, 46);
            this.indexLabel = UIFactory.textField("", 2, 0, 20, 20, _middleLayer, new GTextFormat(FontUtil.songtiName, 11, 16777215));
            this.indexLabel.autoSize = TextFieldAutoSize.LEFT;
            this._txtSkillTypeName = UIFactory.textField("", 20, 20, 20, 20, _middleLayer);
            this._txtSkillTypeName.mouseEnabled = false;
            this._txtSkillTypeName.textColor = 16711680;
            var _loc_1:* = GlobalStyle.textFormatPutong;
            _loc_1.color = 16777215;
            _loc_1.font = FontUtil.songtiName;
            _loc_1.size = 11;
            _loc_1.align = TextFormatAlign.RIGHT;
            this._txtNum = UIFactory.textField("", 2, 0, 40, 18, _middleLayer, _loc_1);
            this._txtNum.mouseEnabled = false;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this.indexLabel.dispose(param1);
            this._txtSkillTypeName.dispose(param1);
            return;
        }// end function

        override protected function updateView() : void
        {
            super.updateView();
            if (this._selectFilter)
            {
                this._selectFilter.width = _width + 4;
                this._selectFilter.height = height + 4;
            }
            return;
        }// end function

        public function set canUseSkill(param1:Boolean) : void
        {
            this._bmpCanNotUse.visible = param1;
            return;
        }// end function

        public function IndexLabelTxt(param1:int) : void
        {
            this.indexLabel.htmlText = "<font color=\'#ffffff\'>" + param1 + "</font>";
            return;
        }// end function

        public function setLabelTxt(param1:String) : void
        {
            this.indexLabel.htmlText = "<font color=\'#ffffff\'>" + param1 + "</font>";
            return;
        }// end function

        override public function set dragSource(param1:Object) : void
        {
            var _loc_2:SkillInfo = null;
            var _loc_3:TPetConfig = null;
            _dragSource = param1;
            if (param1)
            {
                if (param1 is SkillInfo)
                {
                    _loc_2 = param1 as SkillInfo;
                    if (1)
                    {
                        this.source = _loc_2.tSkill.skillIcon + ".jpg";
                        updateCDEffect(param1, CDDataType.skillInfo);
                    }
                    else
                    {
                        this.detory();
                    }
                }
                else if (param1 is ItemData)
                {
                    if (ItemData(param1).itemInfo != null)
                    {
                        this.source = ItemData(param1).itemInfo.url;
                    }
                    updateCDEffect(param1, CDDataType.itemData);
                    DisplayUtil.removeMe(this._txtSkillTypeName);
                }
                else if (param1 is SPet)
                {
                    _loc_3 = PetConfig.instance.getInfoByCode(SPet(param1).publicPet.code);
                    this.source = _loc_3.avatar + ".png";
                    DisplayUtil.removeMe(this._txtSkillTypeName);
                }
                else
                {
                    this.source = null;
                    updateCDEffect();
                    DisplayUtil.removeMe(this._txtSkillTypeName);
                }
                ToolTipsManager.register(this);
            }
            else
            {
                DisplayUtil.removeMe(this._txtSkillTypeName);
                this.source = null;
                updateCDEffect();
                this.source = null;
                ToolTipsManager.unregister(this);
            }
            this.updateIsCanNotAttackMask();
            this.updateLockStatus();
            this.updateNum();
            return;
        }// end function

        public function updateNum() : void
        {
            this._txtNum.text = "";
            if (_dragSource != null && _dragSource is ItemData)
            {
                this._txtNum.text = Cache.instance.pack.backPackCache.getAllAoumtOfSameNameItem(ItemData(_dragSource).itemInfo.name).toString();
                this._txtNum.x = _width - this._txtNum.width - 2;
                this._txtNum.y = _height - 18;
            }
            return;
        }// end function

        public function updateIsCanNotAttackMask() : void
        {
            return;
        }// end function

        public function set addCanNotAttackMaskValue(param1:Boolean) : void
        {
            if (param1)
            {
                this.addCanNotAttackMask();
            }
            else
            {
                this.removeCanNotAttackMask();
            }
            return;
        }// end function

        override public function get toolTipData()
        {
            if (this.dragSource is SPet)
            {
                return null;
            }
            return this.dragSource;
        }// end function

        override public function get isDragAble() : Boolean
        {
            return _dragAble;
        }// end function

        override public function set isDragAble(param1:Boolean) : void
        {
            _dragAble = param1;
            return;
        }// end function

        override protected function onMouseDown(event:MouseEvent) : void
        {
            if (this.isDragAble && this.dragSource)
            {
                DragManager.instance.startDragItem(this, this.bitmapdata);
            }
            return;
        }// end function

        public function detory() : void
        {
            super.unRegisterEffects();
            _cdData = null;
            this.dragSource = null;
            this._txtSkillTypeName.text = "";
            return;
        }// end function

        private function onMouseOverHandler(event:MouseEvent) : void
        {
            this.addChild(this._selectFilter);
            return;
        }// end function

        private function onMouseOutHandler(event:MouseEvent) : void
        {
            this.removeChild(this._selectFilter);
            return;
        }// end function

        public function removeMouseOverOutListener() : void
        {
            this.removeEventListener(MouseEvent.MOUSE_OVER, this.onMouseOverHandler);
            this.removeEventListener(MouseEvent.MOUSE_OUT, this.onMouseOutHandler);
            return;
        }// end function

        override protected function cdFinishedHandler() : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarCDFinished, _pos));
            return;
        }// end function

        public static function get bmpdCanNotUse() : BitmapData
        {
            if (!_bmpdCanNotUse)
            {
                _bmpdCanNotUse = new BitmapData(36, 36);
                _bmpdCanNotUse.fillRect(new Rectangle(0, 0, 36, 36), 2566914048);
            }
            return _bmpdCanNotUse;
        }// end function

    }
}
